﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormAddOrder : FormTemplet
    {
        private int _nCustomerID = -1;

        private int _nProjectID = -1;

        public FormAddOrder()
        {
            this.InitializeComponent();
            this.dtpDeliveryDate.Value = this.dtpSpeakForDate.Value.AddDays(8.0);
        }

        public FormAddOrder(int nCustomerID, string sCustomerName, int nProjectID, string sProjectName) : this()
        {
            this._nCustomerID = nCustomerID;
            this.txtCustomerName.Text = sCustomerName;
            this._nProjectID = nProjectID;
            this.txtProjectName.Text = sProjectName;
        }

        private void btnSelectCustomer_Click(object sender, EventArgs e)
        {
            CustomerData customerData = FormSelectCustomer.ShowBox();
            if (customerData != null)
            {
                this._nCustomerID = customerData.CustomerID;
                this.txtCustomerName.Text = customerData.CompanyName + "(" + customerData.Linkman + ")";
                this._nProjectID = -1;
                this.txtProjectName.Text = "";
                this.btnSelectProject.Focus();
            }
        }

        private void btnSelectProject_Click(object sender, EventArgs e)
        {
            ProjectData projectData = FormSelectProject.ShowBox(this._nCustomerID);
            if (projectData != null)
            {
                this._nProjectID = projectData.ProjectID;
                this.txtProjectName.Text = projectData.ProjectName + "(" + projectData.Linkman + ")";
                this.txtRemark.Focus();
            }
        }

        private void ckSystemDate_Click(object sender, EventArgs e)
        {
            if (this.ckSystemDate.Checked)
            {
                this.dtpSpeakForDate.Enabled = true;
                this.ckSystemDate.Checked = false;
            }
            else
            {
                this.dtpSpeakForDate.Enabled = false;
                this.ckSystemDate.Checked = true;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (DateTime.Now.Date.CompareTo(FormMain.s_TERMINATE_DATE) >= 0)
            {
                if (FormMain.s_isProbation)
                {
                    MessageBox.Show("试用期限已到！如需继续使用，请与软件开发商联系。", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else
                {
                    MessageBox.Show("I/O 错误，导致获得应用程序必要信息的配置文件读取错误，此程序可能不能正常运行\r\n可能是有人 更改 或 删除 了此程序安装路径的文件", "错误！", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                base.Close();
            }
            else if (this.txtProjectName.Text.Trim() == "" || this._nProjectID == -1)
            {
                MessageBox.Show("请选择对此客户的计划项目", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.btnSelectProject.Focus();
            }
            else if (this.txtRemark.Text.Trim().Length > 64)
            {
                MessageBox.Show("备注字符太长", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtRemark.Focus();
            }
            else
            {
                Orders orders = new Orders();
                string text = null;
                string text2 = "";
                try
                {
                    text = orders.InsertOrder("", this._nProjectID, this.dtpSpeakForDate.Value.ToString("yyyy-MM-dd HH:mm:ss"), this.dtpDeliveryDate.Value.ToString("yyyy-MM-dd HH:mm:ss"), this.ckLailiao.Checked, this.ckQuliao.Checked, this.txtRemark.Text.Trim(), LoginInfo.OperatorID, ref text2);
                }
                catch (SqlException ex)
                {
                    if (ex.Number == 50000)
                    {
                        MessageBox.Show(ex.Message, "提示！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                    else
                    {
                        MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                orders.Dispose();
                if (text != null)
                {
                    if (text2.Length > 0)
                    {
                        MessageBox.Show(text2, "Successed", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                    FormOrderInfo formOrderInfo = new FormOrderInfo(text);
                    formOrderInfo.MdiParent = base.MdiParent;
                    formOrderInfo.TopMost = true;
                    formOrderInfo.ShowDialog();
                    base.Close();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void ckQuliao_CheckedChanged(object sender, EventArgs e)
        {
            if (this.ckQuliao.Checked)
            {
                this.ckLailiao.Checked = false;
            }
        }

        private void ckLailiao_CheckedChanged(object sender, EventArgs e)
        {
            if (this.ckLailiao.Checked)
            {
                this.ckQuliao.Checked = false;
            }
        }
    }
}
